import java.awt.Image;
import intro.ex9.*;
/**
 * an abstract class for all of the computer controlled space ships 
 * @author sivandaniel
 *
 */
public abstract class ComputerControlled extends SpaceShip{
	
	//computer controlled ships always accelerate
	private static final boolean ACCELL = true;
	protected static final double CLOSEST_SHIP_CONDITION=0.2;
	
	/**
	 * construct a new computer controlled spaceship
	 */
	public ComputerControlled(){
		super();
	}
	
	@Override
	public abstract void doAction(SpaceWars game);

	@Override
	public Image getImage() {
		if(this.shield){
			 return GameGUI.ENEMY_SPACESHIP_IMAGE_SHIELD;
		}
		return GameGUI.ENEMY_SPACESHIP_IMAGE;
	}
	
	/**
	 * a method that is used by "active" computer controlled spaceships in order to go after the closest space ship
	 * @param game the game object to which this ship belongs.
	 */
	protected void runFromSpaceShip(SpaceWars game){
		
		//where the ship goes to
		int shipGoTo=DONT_TURN;
		
		//pointer to the closest ship to this
		SpaceShip nearShip=game.getClosestShipTo(this);
		
		//the angle of closest ship
		double nearShipAngle=this.getPhysics().angleTo(nearShip.getPhysics());
		
		//check where other ship is going and change this ship direction
		if(nearShipAngle>DONT_TURN){
			shipGoTo=TURN_RIGHT;
		}
		else if(nearShipAngle<DONT_TURN){
			shipGoTo=TURN_LEFT;
		}
		
		//change this ship direction
		this.pos.move(ACCELL, shipGoTo);
		
	}
	
	/**
	 * a method that is used by "active" computer controlled spaceships in order to go after the closest space ship
	 * @param game the game object to which this ship belongs.
	 */
	protected void goAfterSpaceShip(SpaceWars game){
		
		//where the ship goes to
		int shipGoTo=DONT_TURN;
		
		//the closest ship to this
		SpaceShip nearShip=game.getClosestShipTo(this);
		
		//the angle of closest ship
		double nearShipAngle=this.getPhysics().angleTo(nearShip.getPhysics());
		
		//check where other ship is going and change this ship direction
		if(nearShipAngle>DONT_TURN){
			shipGoTo=TURN_LEFT;
		}
		else if(nearShipAngle<DONT_TURN){
			shipGoTo=TURN_RIGHT;
		}
		
		//change this ship direction
		this.pos.move(ACCELL, shipGoTo);
		
	}
}
